<template>
  <el-dialog v-model="dialogFormVisible" title="编辑用户">
    <el-form :model="form">
      <el-form-item label="用户名" :label-width="formLabelWidth">
        <el-input v-model="form.username" disabled autocomplete="off" />
      </el-form-item>

      <el-form-item label="邮箱" :label-width="formLabelWidth">
        <el-input v-model="form.email" autocomplete="off" />
      </el-form-item>
      <el-form-item label="电话" :label-width="formLabelWidth">
        <el-input v-model="form.mobile" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="getFrom">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, defineEmits } from "vue";
import { EditUser } from "@/api/Users";
import { fromPairs } from "lodash";
// 调用父组件方法并传入表单数据
const emit = defineEmits(["getEdit"]);
const getFrom = async () => {
  dialogFormVisible.value = false;
  console.log(form);
  let res = await EditUser(form.id as number, {
    email: form.email,
    mobile: form.mobile,
  });
  console.log(res, "编辑信息res");

  emit("getEdit", num.value, form);
};

const dialogTableVisible = ref(false);
// 打开弹窗
const dialogFormVisible = ref<boolean>(false);
const formLabelWidth = "140px";

const form: { username: string; email: string; mobile: string; id?: number } =
  reactive({
    username: "",
    email: "",
    mobile: "",
  });
const num = ref(0);
const openDialog = (
  index: number,
  row: { username: string; email: string; mobile: string; id: number }
) => {
  console.log(row, index, "父组件传入的参数");
  dialogFormVisible.value = true;
  num.value = index;
  form.username = row.username;
  form.email = row.email;
  form.mobile = row.mobile;
  form.id = row.id;
};
// 将子组件的方法暴露出去
defineExpose({
  openDialog,
});
</script>
<style scoped>
.el-table .el-table__cell.is-right {
  text-align: left !important;
}
.el-button--text {
  margin-right: 15px;
}
.el-select {
  width: 300px;
}
.el-input {
  width: 300px;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
